package com.atguigu.srb.core.controller.api;


import com.atguigu.common.results.R;
import com.atguigu.common.results.ResultCodeEnum;
import com.atguigu.srb.base.utils.JwtUtils;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.vo.LoginVo;
import com.atguigu.srb.core.pojo.vo.UserIndexVo;
import com.atguigu.srb.core.pojo.vo.UserInfoVo;
import com.atguigu.srb.core.pojo.vo.UserVo;
import com.atguigu.srb.core.service.UserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2022-10-24
 */
@RestController
@Slf4j
@RequestMapping("/api/core/userInfo")

public class ApiUserInfoController {
    @Autowired
    UserInfoService userInfoService;

    @ApiOperation("登录成功查询账户回显数据")
    @GetMapping("auth/getUserIndexVo")
    //@RequestHeader("token") String token  希望springmvc从请求头中获取token，而且默认头是必须的
    public R getUserIndexVo(@RequestHeader(value = "token" , required = false) String token){
        UserIndexVo userIndexVo = userInfoService.getUserIndexVo(token);
        return R.success().data("item",userIndexVo);
    }

    @PostMapping("test1")
    public R test1(@RequestBody R r){//从请求体获取json转为R对象
        log.info(r.toString());
        return R.success().message("test1");
    }
    @PostMapping("test2")
    public R test2(String username,String password){
        log.info(username+","+ password);
        return R.success().message("test2");
    }
    @PostMapping("test3")
    public R test3(R r){//pojo入参 从请求参数中获取属性名的值设置给对象
        log.info(r.toString());
        return R.success().message("test3");
    }



    @ApiOperation("判断手机号是否已注册")
    @GetMapping("isRegisted/{mobile}")
    public R isRegisted(@PathVariable("mobile")String mobile){
//        try {
//            //休眠3秒 模拟业务执行时间较长
//            TimeUnit.SECONDS.sleep(3);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        int count = userInfoService.count(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getMobile, mobile));
        //返回true代表手机号可用  否则不可用
        return R.success().data("flag" , count==0);
    }

    @ApiOperation("验证token是否有效")
    @GetMapping("checkToken")
    public R checkToken(@RequestHeader("token")String token){
        boolean b = JwtUtils.checkToken(token);
        if(b){
            //token校验成功
            return R.success();
        }else{
            //token校验失败
            return R.setResult(ResultCodeEnum.LOGIN_AUTH_ERROR);
        }
    }
    @ApiOperation("根据token获取用户信息")
    @GetMapping("getUserInfo")
    public R getUserInfo(@RequestHeader("token")String token){
        //约定 前端token通过请求头提交
        //从jwt中解析出用户的id，查询用户的信息 获取 头像 id  昵称交给前端回显
        Long userId = JwtUtils.getUserId(token);
        UserInfo userInfo = userInfoService.getById(userId);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(userInfo , userVo);
        return R.success().data("item",userVo);
    }
    //登录接口
    @ApiOperation("用户登录")
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo, HttpServletRequest request){
        //登录成功返回jwt
        String host = request.getRemoteHost();
        String token = userInfoService.login(loginVo,host);
        return R.success().data("token",token);
    }
    //注册接口
    @PostMapping("register")
    public R register(@RequestBody UserInfoVo userInfoVo){
        userInfoService.register(userInfoVo);
        return R.success().message("注册成功");
    }

}

